#include <GUI/Widgets/TimeWidget.hpp>
#include <FL/fl_draw.H>
#include <FL/Fl_Group.H>
#include <GUI/Theme.hpp>
#include <cstdio>

TimeWidget::TimeWidget(int X, int Y, int W, int H, const char* label)
    : Fl_Widget(X, Y, W, H, label)
{
}

void TimeWidget::set_hour(uint8_t h)
{
    if (h > 24) h = 24;
    if (h == hour_) return;
    hour_ = h;
    redraw();
}

void TimeWidget::draw()
{
    fl_push_clip(x(), y(), w(), h());
    Fl_Color bg = FL_WHITE;
    if (parent()) bg = parent()->color();
    fl_color(bg);
    fl_rectf(x(), y(), w(), h());

    const auto& th = GUI::current_theme();

    char buf[64];
    std::snprintf(buf, sizeof(buf), "%u:00", static_cast<unsigned>(hour_));

    fl_font(FL_HELVETICA_BOLD, 28);
    fl_color(th.text_primary);
    fl_draw(buf, x(), y(), w(), h(), FL_ALIGN_CENTER);

    fl_font(FL_HELVETICA, 12);
    fl_color(th.text_secondary);
    fl_draw("TIME", x(), y() + h() - 18, w(), 16, FL_ALIGN_BOTTOM | FL_ALIGN_CENTER);

    fl_pop_clip();
}
